import { defineStore } from 'pinia';
import { deepCopy } from '@/utils/utils.ts';

interface ConstValueType {
  projectID: number;
  companyID: number;
  flowID: number;
}
export const useConstValue = defineStore('constValue', {
  state: (): ConstValueType => ({
    projectID: 224,
    companyID: 82,
    flowID: 23,
  }),
  persist: {
    storage: sessionStorage,
  },
});
export const useNodeValue = defineStore('nodeValue', {
  state: (): any => ({
    typeObj:{},
    nodeuserCount:[],
    eventID:null,
    nodeId:null,
    title:''
  }),
  persist: {
    storage: sessionStorage,
  },
});
interface MainUserType {
  id: number | null;
  project: number | null;
  space: number | null;
  type: '业主' | '家属' | '租客';
  user: number | null;
  [key: string]: any;
}
interface LocalValueType {
  r_id: '' | string;
  mainUser: MainUserType;  //当前r_id的业主ID
}
export const useLocalValue = defineStore('localValue', {
  state: (): LocalValueType => ({
    r_id: '',
    mainUser: {
      id: null,
      project: null,
      space: null,
      type: '租客',
      user: null,
      user_identity: '无',
    },
  }),
  actions: {
    setR_id(msg: string): void {
      this.r_id = msg;
    },
    setMainUser(msg: Partial<MainUserType>): void {
      for (const key in this.mainUser) {
        if (Object.prototype.hasOwnProperty.call(msg, key)) {
          this.mainUser[key] = msg[key]!;
        }
      }
    },
  },
  persist: {
    storage: sessionStorage,
  },
});

interface RolesType {
  id: number;
  name: string;
  parent_id: null | number;
  company_id: number;
  project_id: null | number;
  [key: string]: any;
}
interface IuserInfoType {
  admin_projects2: number[];          //主题2使用，是哪个项目的管理员---在项目的admin_user设置成了该用户，
  company_functions: string[];        //公司权限
  company_roles: RolesType[];         //公司角色
  created_company: number | null;     //不为null 则是超管
  info: {
    company: number | null;           //此人所在的公司id
    company_logo: string;             //此人所在的公司logo
    company_name: string;             //此人所在的公司名称
    company_superuser: number | null; //公司的超管id
    contract: string;                 //联系方式
    department: {
      addr?: string;                  //所属组织的地址
      code?: string;                  //所属组织的编码
      company?: number;               //所属组织的所属公司
      department?: number;             //所属组织的id
      dtype?: number;                 //所属组织的组织类型
      fzr?: string;                   //所属组织的负责人
      name?: string;                  //所属组织的名称
      parent?: number | null;         //所属组织的上级组织
      phone?: string;                 //所属组织的联系电话
    };
    id: number | null;                //当前用户id
    logout_time: string;              //登出时间
    openid: boolean;                  //有么有公众号吧
    projects: any[];                  //在哪些项目下
    truename: string;                 //真实姓名
    type: '注册用户' | '公司用户' | '项目用户' | '恒济用户' | '合约用户'; //是什么类型用户
    units_flag: string;               //空字符串或'[project_id]'的json数组
  };
  project_functions: {
    [key: number]: string[];          //项目下的权限
  };
  project_roles: RolesType[];         //项目下的权限
  projects: number[];                 //主题1使用，是哪个项目的管理员---在公司角色权限那里此人所在的角色勾了哪些项目,
  style_m: {
    id: number | null;                //移动端主题id
    ifile: string;                    //移动端主题缩略图
    name: string;                     //移动端主题名称
    type: string;                     //类型
  };
  units: number[];                    //拥有哪些站点的权限
  [key: string]: any;                 //未知属性
}
export const useIuserInfoStore = defineStore('iuserInfo', {
  state: (): IuserInfoType => ({
    admin_projects2: [],
    company_functions: [],
    company_roles: [],
    created_company: null,
    info: {
      company: null,
      company_logo: '',
      company_name: '',
      company_superuser: null,
      contract: '',
      department: {},
      id: null,
      logout_time: '',
      openid: false,
      projects: [],
      truename: '',
      type: '项目用户',
      units_flag: '',
    },
    project_functions: {},
    project_roles: [],
    projects: [],
    style_m: {
      id: null,
      ifile: '',
      name: '',
      type: '',
    },
    units: [],
    userSystemInfo:{}
  }),
  getters: {
    
  },
  actions: {
    setIuserInfo(msg: Partial<IuserInfoType>): void {
      for (const key in this) {
        if (Object.prototype.hasOwnProperty.call(msg, key)) {
          this[key] = msg[key]!;
        }
      }
    },
  },
  persist: {
    storage: sessionStorage,
  },
});

interface ProjectType {
  id: number;
  company: number;
  name: string;
  used: boolean;
  type: string;
  [key: string]: any;
}
interface CompanyType {
  id: number;
  name: string;
  style: number;
  style_m: number;
  projects: ProjectType[];
  [key: string]: any;
}
interface CurrentType {
  projectsOfCompany: CompanyType[];
  project: ProjectType | null;  //当前项目
  company: CompanyType | null;  //当前公司
  paramsTree: null;             //当前参数配置复制入剪贴板的结构
}
export const useCurrentStore = defineStore('current', {
  state: (): CurrentType => ({
    projectsOfCompany: [],
    project: null,
    company: null,
    paramsTree: null,
  }),
  getters: {
    getAllCompanys(state) {
      return deepCopy(state.projectsOfCompany);
    },
    getUsedCompanys(state) {
      const arr = deepCopy(state.projectsOfCompany);
      arr.forEach(c => {
        c.projects = c.projects.filter(p => p.used && p.type === '项目');
      });
      const companys = arr.filter(c => c.projects.length) as any;
      companys.forEach((c: any) => {
        delete c.projects;
      })
      return companys;
    },
    getUsedProjectsOfCompany(state) {
      const arr = deepCopy(state.projectsOfCompany);
      arr.forEach(c => {
        c.projects = c.projects.filter(p => p.used && p.type === '项目');
      });
      return arr.filter(c => c.projects.length);
    }
  },
  actions: {
    updateSomeProjectOfCompany(project: ProjectType, isChange: boolean) {
      this.projectsOfCompany.forEach(c => {
        const index = c.projects.findIndex(p => p.id === project.id);
        if (index !== -1) {
          c.projects.splice(index, 1);
        }
        if (isChange && c.id === project.company) {
          c.projects.push(project);
        }
      });
    },
    setProjectsOfCompany(data: CompanyType[]) {
      this.projectsOfCompany = data;
    },
    setProject(data: ProjectType | null) {
      this.project = data;
    },
    setCompany(data: CompanyType) {
      this.company = data;
    },
    setParamsTree(data: any) {
      this.paramsTree = data;
    }
  },
  persist: {
    storage: localStorage,
  },
});